<?php
include('../includes/config.inc');
include('../includes/variables.inc');
mysql_connect($DB_HOST, $DB_VIEW_USER, $DB_VIEW_USER_PASS);
mysql_select_db($DB_NAME);

include ('../includes/class.ezpdf.php');
$pdf =& new Cezpdf();
$pdf->selectFont('../includes/fonts/Helvetica.afm');

$pdf->ezColumnsStart();

if ($_POST['print_id']) {
	$id_counter = 0;
	foreach ($_POST['print_id'] as $print_id){
		$cdid_array[$id_counter++] = $print_id;
	}
}

$ColumnLimit = $_POST['ColumnLimit'];
$CountForColumns = 1;
$i_counter = 0;
while ($i_counter < count($cdid_array)) {
	$pdfQuery = mysql_query("SELECT * FROM `album` WHERE `cddbid`='".$cdid_array[$i_counter++]."' ORDER BY `slot` DESC LIMIT 1");
	while ($pdfResult = mysql_fetch_array($pdfQuery)) {
		$data = array(
		array(
		'main' => ($pdfResult['artist'] . '  <i>' . $pdfResult['title'] . '</i>'), 
		'slotno' => ($pdfResult['slot'])
		)
		);
		$pdf->ezTable($data, array('main'=>'main','slotno'=>'slotno'), '', 
	   array('showHeadings'=>0, 'shaded'=>0, 'width'=>260, 'cols' => 
		  array('main'=>
			 array('width'=>230,'justification'=>'left')
		  ), 
		  array('slotno'=> 
		array('width'=>30,'justification'=>'right')
		  ) 
		)
		);

		$description = array(
		array($pdfResult['review'])
		);
		$pdf->ezTable($description,'','',array('width'=>260, 'justification'=>'right','showHeadings'=>0));
      
		$GoodCuts = 'Good Cuts: ';
		$CutsQuery = mysql_query("SELECT * FROM `track` WHERE (`cddbid`='".$pdfResult['cddbid']."') AND (`virtue`>'3') ");
		while($pdfTrackResult = mysql_fetch_array($CutsQuery)) {
		   if ($pdfTrackResult[virtue] >= 5) $GoodCuts = $GoodCuts . '<b>';
		   if ($pdfTrackResult[virtue] >= 6) $GoodCuts = $GoodCuts . '<u>';
		   $GoodCuts = $GoodCuts . $pdfTrackResult[trackno];
		   if ($pdfTrackResult[virtue] >= 6) $GoodCuts = $GoodCuts . '</u>';
		   if ($pdfTrackResult[virtue] >= 5) $GoodCuts = $GoodCuts . '</b>';
		   $GoodCuts = $GoodCuts . ' ';
		}
		if ($pdfResult[multi_id]) {
		   $findMultiQuery = mysql_query("SELECT * FROM `album` WHERE `multi_id`='".$pdfResult[cddbid]."' ORDER BY `multi` ASC ");
		   while ($findMulti = mysql_fetch_array($findMultiQuery)) {
		      $GoodCuts .= "\nDisc ". $findMulti[multi].': ';
            $CutsQuery = mysql_query("SELECT * FROM `track` WHERE (`cddbid`='".$findMulti['cddbid']."') AND (`virtue`>'3') ");
		      while($pdfTrackResult = mysql_fetch_array($CutsQuery)) {
		         if ($pdfTrackResult[virtue] >= 5) $GoodCuts = $GoodCuts . '<b>';
		         if ($pdfTrackResult[virtue] >= 6) $GoodCuts = $GoodCuts . '<u>';
		         $GoodCuts = $GoodCuts . $pdfTrackResult[trackno];
		         if ($pdfTrackResult[virtue] >= 6) $GoodCuts = $GoodCuts . '</u>';
		         if ($pdfTrackResult[virtue] >= 5) $GoodCuts = $GoodCuts . '</b>';
		         $GoodCuts = $GoodCuts . ' ';
		      }
		   
		   }
		}
		$GoodCuts = array(array($GoodCuts)); 
		$pdf->ezTable($GoodCuts,'','',array('width'=>260, 'justification'=>'left','showHeadings'=>0));

		$AvoidCuts = 'Avoid: ';
		$CutsQuery = mysql_query("SELECT * FROM `track` WHERE (`cddbid`='".$pdfResult['cddbid']."') AND (`virtue`<'3') ");
		while($pdfTrackResult = mysql_fetch_array($CutsQuery)) {
		  $AvoidCuts = $AvoidCuts . $pdfTrackResult[trackno] . ' ';
		}
		if ($AvoidCuts === 'Avoid: ') $AvoidCuts = 'Avoid: none (all clean)';
		if ($pdfResult[multi_id]) {
		   $findMultiQuery = mysql_query("SELECT * FROM `album` WHERE `multi_id`='".$pdfResult[cddbid]."' ORDER BY `multi` ASC ");
//		   $m = 0; // iterator 
		   while ($findMulti = mysql_fetch_array($findMultiQuery)) {
            $avoidCutsMulti = '';
            $CutsQuery = mysql_query("SELECT `trackno`,`virtue` FROM `track` WHERE (`cddbid`='".$findMulti['cddbid']."') AND (`virtue`<'3') ");
            while ($avoidResult = mysql_fetch_array($CutsQuery)) {
               $avoidCutsMulti .= $avoidResult[trackno].' ';
            }
      		if (!$avoidCutsMulti || $avoidCutsMulti === '') $avoidCutsMulti = 'none (all clean)';
		      $AvoidCuts .= "\nDisc ".$findMulti[multi].': ' . $avoidCutsMulti;
		   }
		}
		
		$AvoidCuts = array(array($AvoidCuts)); 
		$pdf->ezTable($AvoidCuts,'','',array('width'=>260, 'justification'=>'left','showHeadings'=>0));
		$pdf->ezText(' ');
	}
	if ($CountForColumns == $ColumnLimit) {
		$pdf->ezNewPage();
		$CountForColumns = 1;
	} else {
		$CountForColumns++;
	}
	$pageLimit++;
}
$pdf->ezStream();
?>